package com.acwing.chapter3;

import java.io.*;

/**
 * @author anan
 * @create 2021-08-26 20:37
 */
public class liantongkuai {
    private static int N = 100010;
    private static int[] p = new int[N];
    private static int[] size = new int[N];

    private static int find(int x){
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] str1 = reader.readLine().split(" ");
        int n = Integer.parseInt(str1[0]);
        int m = Integer.parseInt(str1[1]);

        for (int i = 1; i <= n; i++) {
            p[i] = i;
            size[i] = 1;
        }
        while (m-- > 0){
            String[] str2 = reader.readLine().split(" ");
            int a;
            int b;
            int x;
            if ("C".equals(str2[0])){
                a = Integer.parseInt(str2[1]);
                b = Integer.parseInt(str2[2]);
                if (find(a) == find(b)) continue;
                size[find(a)] += size[find(b)];
                p[find(b)] = find(a);
            }
            else if ("Q1".equals(str2[0])){
                a = Integer.parseInt(str2[1]);
                b = Integer.parseInt(str2[2]);
                if (find(a) == find(b)) writer.write("Yes" + "\n");
                else writer.write("No" + "\n");
            }
            else if ("Q2".equals(str2[0])){
                x = Integer.parseInt(str2[1]);
                writer.write(size[find(x)] + "\n");
            }
        }
        writer.flush();
        writer.close();
        reader.close();
    }
}
